package com.ele.web;

import com.ele.pojo.Deliveryaddress;
import com.ele.pojo.ResponseData;
import com.ele.pojo.User;
import com.ele.service.DeliveryaddressService;
import com.ele.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;

@RestController
@Tag(name="UserController",description = "用户信息")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private DeliveryaddressService deliveryaddressService;

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public ResponseData<Void> userRegister(@RequestBody @Validated User user){
        return this.userService.userRegister(user);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    @Parameter(name="username",description = "用户名")
    @Parameter(name="password",description = "密码")
    @ApiResponse(description = "获取菜品" ,
            content = @Content(mediaType="application/json",
                    schema=@Schema(implementation = ResponseData.class)))
    public ResponseData<User> userLogin(@NotEmpty String username,@NotEmpty String password){
        return this.userService.userLogin(username,password);
    }

    /**
     * 加载用户的默认收货地址
     */
    @GetMapping("/getReceiveAddress")
    public ResponseData<Deliveryaddress> getReceiveAddress(@NotEmpty String userId){
        return this.deliveryaddressService.getReceiveAddress(userId);
    }

    /**
     * 查询当前登录用户的所有收货地址
     */
    @GetMapping("/getReceiveAddressAll")
    public ResponseData<Deliveryaddress> getReceiveAddressAll(@NotEmpty String userId){
        return this.deliveryaddressService.getReceiveAddressAll(userId);
    }
}